# try to link static libc and other static libraries used
# Under MSVC use /MT instead of /MD
# Under GCC use -static-libgcc instead of -shared-libgcc and also we define -static
# only msvc and gcc supported

if(NOT __LINK_STATIC_LIBC_DEFINED)
set(__LINK_STATIC_LIBC_DEFINED TRUE)
#-------------------------------------------------------------------------
# give out an option to turn the static link off
if(NOT LINK_SHARED_LIBC)
  option(LINK_SHARED_LIBC "Link Dynamically with the libc library" OFF)
endif()

include("${CMAKE_CURRENT_LIST_DIR}/cmkutils.cmake")

if(NOT LINK_SHARED_LIBC)
  if(WIN32 AND MSVC)
    util_remove_cflags("/MD(d)?" "/MT(d)?")
    util_add_cflags(debug "/MTd")
    util_add_cflags(optimized "/MT")
  else()
    util_remove_ldflags("-shared-libgcc" "-shared-libstdc[+][+]")
    util_add_ldflags("-static" "-static-libgcc" "-static-libstdc++")
  endif()
else()
  # compilers default do the right things --> dynamic link, but we do it 
  # explicitly to make sure things happen as expected
  if(WIN32 AND MSVC)
    util_remove_cflags("/MD(d)?" "/MT(d)?")
    util_add_cflags(debug "/MDd")
    util_add_cflags(optimized "/MD")
  else()
    util_remove_ldflags("-static" "-static-libgcc" "-static-libstdc[+][+]")
    util_add_ldflags("-shared-libgcc" "-shared-libstdc++")
  endif()  
endif()
#-------------------------------------------------------------------------
endif()
